package org.example.blog.utils;

import io.jsonwebtoken.Claims;
import org.example.blog.pojo.User;

import java.util.HashMap;
import java.util.Map;

public class ClaimsUtils {

    public static final String ID = "id";
    public static final String USERNAME = "username";
    public static final String ROLES = "roles";
    public static final String AVATAR = "avatar";
    public static final String EMAIL = "email";
    public static final String SIGN = "sign";


//        收集用户设置token之前的token数据的封装
    public static Map<String, Object> userClaims(User user) {
        Map<String, Object> claims = new HashMap<>();
        claims.put(ID, user.getId());
        claims.put(USERNAME, user.getUsername());
        claims.put(ROLES, user.getRoles());
        claims.put(AVATAR, user.getAvatar());
        claims.put(EMAIL, user.getEmail());
        claims.put(SIGN, user.getSign());
        return claims;
    }

    // 解析token后的封装
    public static User claimsUser(Claims claims){
        User user = new User();
        String id = (String) claims.get(ID);
        user.setId(id);
        String username = (String) claims.get(USERNAME);
        user.setUsername(username);
        String roles = (String) claims.get(ROLES);
        user.setRoles(roles);
        String avatar = (String) claims.get(AVATAR);
        user.setAvatar(avatar);
        String email = (String) claims.get(EMAIL);
        user.setEmail(email);
        String sign = (String) claims.get(SIGN);
        user.setSign(sign);
        return user;
    }
}
